package com.chixing.ih.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘岩
 * @version 1.0
 * @Desc Practice makes perfect
 * @date 2020/9/11 13:36
 */
public class FileServerUtil {

    private static final String HTTP = "http://";
    private static final String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static final String accessKeyId = "LTAI4GHTGzn8AcAu4KBReemn";
    private static final String accessKeySecret = "MwcbUcGaHrNXr33qDVeND8j3f5M0oT";
    private static final String bucketName = "masteryan";


    /**
     * 获取Oss 对象
     *
     * @return oss-cn-beijing.aliyuncs.com
     */
    public static OSS getOSS() {
        return new OSSClient(HTTP + endpoint, accessKeyId, accessKeySecret);
    }


    /**
     * 上传文件
     *
     * @param file
     * @param fileName 文件名称
     * @return
     */

    public static String uploadFile(MultipartFile file, String fileName) {
        try {
            OSS oss = getOSS();
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, file.getInputStream());
            oss.putObject(putObjectRequest);
            oss.shutdown();
            return HTTP + bucketName + "." + endpoint + "/" + fileName;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param fileName 文件名称
     * @param os
     * @return
     * @throws IOException
     */

    //通知浏览器以附件形式下载
//        response.setHeader("Content-Disposition",
//                "attachment;filename=" + new String(objectName.getBytes(), "ISO-8859-1"));
    public static void downLoadFile(String fileName, OutputStream os) throws IOException {
        OSS oss = getOSS();
        OSSObject ossObject = oss.getObject(bucketName, fileName);
        BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
        BufferedOutputStream out = new BufferedOutputStream(os);
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = in.read(buffer)) != -1) {
            out.write(buffer, 0, length);
        }
        if (out != null) {
            out.flush();
            out.close();
        }
        if (in != null) {
            in.close();
        }


    }


    /**
     * 删除文件
     *
     * @param fileName 文件名称
     */
    public static void deleteOSS(String fileName) {
        OSS oss = getOSS();
        oss.deleteObject(bucketName, fileName);
        oss.shutdown();
    }


    /**
     * 获取所有的文件
     *
     * @return
     */
    public static List<String> listOSS() {
        OSS oss = getOSS();
        List list = new ArrayList();
        ObjectListing objectListing = oss.listObjects(bucketName);
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            list.add(objectSummary.getKey());
        }
        oss.shutdown();
        return list;
    }


}
